﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Newtonsoft.Json;
using System.Data.Entity.Infrastructure;
using _24_Hour_Blogger.Models;
using _24_Hour_Blogger.Models.Content;
using _24_Hour_Blogger.Library;

namespace _24_Hour_Blogger.ViewModels.Content
{
    public partial class ArticleViewModel : ViewModelBase<Article>
    {

        public ArticleViewModel()
        {
            this.Entity = new Article();
        }

        DbContext dataContext = null;
        DbSet<Article> entitiesCollection = null;
        Article entityClass = null;
        public ArticleViewModel(DbContext dataContext, DbSet<Article> entitiesCollection, Article entityClass)
        //: base(dbContext, entitiesCollection, entityClass)
        {

            this.dataContext = dataContext;
            this.entitiesCollection = entitiesCollection;
            this.entityClass = entityClass;
            if (this.entityClass == null)
            {
                this.entityClass = new Article();
            }
            this.Entity = this.entityClass;
        }

        public override Int32 Id
        {
            get
            {
                return Entity.ArticleId;
            }
            set
            {

                Entity.ArticleId = value;


            }
        }

        public Int32 ArticleId
        {
            get
            {
                return Entity.ArticleId;
            }
            set
            {
                if (value > 0)

                    if (Entity.ArticleId != value)
                    {
                        Entity.ArticleId = value;
                        this.IsDirty = true;
                    }
            }
        }

        public String ArticleName
        {
            get
            {
                return Entity.ArticleName;
            }
            set
            {
                if (Entity.ArticleName != value)
                {
                    Entity.ArticleName = value;
                    this.IsDirty = true;
                }
            }
        }

        public String ArticleUrl
        {
            get
            {
                return Entity.ArticleUrl;
            }
            set
            {
                if (Entity.ArticleUrl != value)
                {
                    Entity.ArticleUrl = value;
                    this.IsDirty = true;
                }
            }
        }

        public String Description
        {
            get
            {
                return Entity.Description;
            }
            set
            {
                if (Entity.Description != value)
                {
                    Entity.Description = value;
                    this.IsDirty = true;
                }
            }
        }

        public String Keywords
        {
            get
            {
                return Entity.Keywords;
            }
            set
            {
                if (Entity.Keywords != value)
                {
                    Entity.Keywords = value;
                    this.IsDirty = true;
                }
            }
        }

        public String Synopsis
        {
            get
            {
                return Entity.Synopsis;
            }
            set
            {
                if (Entity.Synopsis != value)
                {
                    Entity.Synopsis = value;
                    this.IsDirty = true;
                }
            }
        }


        public int BlogId
        {
            get
            {
                return Entity.BlogId;
            }
            set
            {
                if (Entity.BlogId != value)
                {
                    Entity.BlogId = value;
                    this.IsDirty = true;
                }
            }
        }

        public int PrimaryCategoryId
        {
            get
            {
                return Entity.PrimaryCategoryId;
            }
            set
            {
                if (Entity.PrimaryCategoryId != value)
                {
                    Entity.PrimaryCategoryId = value;
                    this.IsDirty = true;
                }
            }
        }

        public int StatusId
        {
            get
            {
                return Entity.StatusId;
            }
            set
            {
                if (Entity.StatusId != value)
                {
                    Entity.StatusId = value;
                    this.IsDirty = true;
                }
            }
        }

        public int UserId
        {
            get
            {
                return Entity.UserId;
            }
            set
            {
                if (Entity.UserId != value)
                {
                    Entity.UserId = value;
                    this.IsDirty = true;
                }
            }
        }

        public override DateTime CreatedDate
        {
            get
            {
                return Entity.CreatedDate;
            }
            set
            {
                if (Entity.CreatedDate != value)
                {
                    Entity.CreatedDate = value;
                    this.IsDirty = true;
                }
            }
        }

        public override DateTime UpdatedDate
        {
            get
            {
                return Entity.UpdatedDate;
            }
            set
            {
                if (Entity.UpdatedDate != value)
                {
                    Entity.UpdatedDate = value;
                    this.IsDirty = true;
                }
            }
        }

        public override Int32 CreatedByUserId
        {
            get
            {
                return Entity.CreatedByUserId;
            }
            set
            {
                if (Entity.CreatedByUserId != value)
                {
                    Entity.CreatedByUserId = value;
                    this.IsDirty = true;
                }
            }
        }

        public override Int32 UpdatedByUserId
        {
            get
            {
                return Entity.UpdatedByUserId;
            }
            set
            {
                if (Entity.UpdatedByUserId != value)
                {
                    Entity.UpdatedByUserId = value;
                    this.IsDirty = true;
                }
            }
        }

        private BlogViewModel _Blog;
        [JsonIgnore]
        public BlogViewModel Blog
        {
            get
            {

                if (_Blog == null)
                {
                    if (this.Entity != null && this.Entity.Blog != null)
                        _Blog = new BlogViewModel(dataContext, DataStore.ContentEntities.Blogs, this.Entity.Blog);
                    else
                        if (dataContext == null)
                        {} //    _Blog = new BlogViewModel();
                        else
                            _Blog = new BlogViewModel(dataContext, DataStore.ContentEntities.Blogs, null);
                }

                return _Blog;
            }
            set
            {
                if (this.dataContext != null)
                    if (this.Entity.Blog != value.Entity)
                    {
                        this.Entity.Blog = value.Entity;
                    }
                if (_Blog != value)
                {
                    _Blog = value;
                }
            }
        }

        private DataCollectionBase<ArticleCommentViewModel, ArticleComment> _ArticleComments;
        [JsonIgnore]
        public DataCollectionBase<ArticleCommentViewModel, ArticleComment> ArticleComments
        {
            get
            {
                if (_ArticleComments == null)
                {
                    if (this.Entity != null)
                        _ArticleComments = new DataCollectionBase<ArticleCommentViewModel, ArticleComment>(dataContext, DataStore.ContentEntities.ArticleComments, this.Entity.ArticleComments);
                    else
                        _ArticleComments = null;
                }

                return _ArticleComments;
            }
            set
            {
                _ArticleComments = value;
            }
        }

    }
}